/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cgi.ndk.wf.activiti;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author kovalcikm
 */
@Service
public class ActivitiService {

    private final static String PROCESS_REST_URL = "http://10.28.6.9:8099/activiti-rest/service/runtime/process-instances";
    private final static String TASK_REST_URL = "http://10.28.6.9:8099/activiti-rest/service/runtime/tasks/";
    private final static String GET_ALL_TASKS_URI = "http://10.28.6.9:8099/activiti-rest/service/management/tables/act_hi_taskinst/data?";
    private final static String GET_PROCESSES_TASKS_URI = "http://10.28.6.9:8099/activiti-rest/service/management/tables/act_hi_procinst/data?";
    private final static int ALL_CONSTANT = 1000000; //TODO better handling

    RestTemplate restTemplate = new RestTemplate();

    public Object getAllProcesses() {
        RestClient restClient = new RestClient();
        Object response = restClient.exchange(GET_PROCESSES_TASKS_URI + ALL_CONSTANT, HttpMethod.GET, null);
        return response;
    }

    public Object startProcess(Object request) {
        RestClient restClient = new RestClient();
        Object response = restClient.exchange(PROCESS_REST_URL, HttpMethod.POST, request);
        return response;
    }

    public Object getProcessDetail(String instanceId) {
        RestClient restClient = new RestClient();
        Object response = restClient.exchange(PROCESS_REST_URL + instanceId, HttpMethod.GET, null);
        return response;
    }

    public Object getAllTasks() {
        RestClient restClient = new RestClient();
        Object response = restClient.exchange(GET_ALL_TASKS_URI + ALL_CONSTANT, HttpMethod.GET, null);
        return response;
    }

    public Object getTaskDetail(String instanceId) {
        RestClient restClient = new RestClient();
        Object response = restClient.exchange(TASK_REST_URL + instanceId, HttpMethod.GET, null);
        return response;
    }

    public Object changeTask(String instanceId, Object changeTask) {
        RestClient restClient = new RestClient();
        Object response = restClient.exchange(TASK_REST_URL + instanceId, HttpMethod.PUT, changeTask);
        return response;
    }

    public Object complete(String instanceId, Object complete) {
        RestClient restClient = new RestClient();
        Object response = restClient.exchange(TASK_REST_URL + instanceId, HttpMethod.POST, complete);
        return response;
    }

    public Object claim(String instanceId, Object claim) {
        RestClient restClient = new RestClient();
        Object response = restClient.exchange(TASK_REST_URL + instanceId, HttpMethod.POST, claim);
        return response;
    }

}
